package com.twu.biblioteca;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Library {

	private List<Book> bookList;
    private List<Movie> movieList;

    public static Library buildLibrary() {
        Library library = new Library();
        library.setBookList(new LinkedList<Book>());
        library.setMovieList(new LinkedList<Movie>());
        return library;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

    public void setMovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }

    public void addBook(Book book) {
		bookList.add(book);
	}

    public void addMovie(Movie movie) {
        movieList.add(movie);
    }

	public List<Book> getBookList() {
		return bookList;
	}

	public int getAvailability(String isbnNumber) {
        return getBookByISBNNumber(isbnNumber).getAvailableCopies();
	}

	public boolean reserveBook(String bookISBNNumber, String customerName) {
		return reserve(getBookByISBNNumber(bookISBNNumber), customerName);
	}

    public boolean reserveMovie(String movieID, String customerName) {
        return reserve(getMovieByID(movieID), customerName);
    }

    public Movie getMovieByID(String movieID) {
        for(int i = 0;i<movieList.size();i++){
            Movie movie = movieList.get(i);
            if(movie.getMovieID().equals(movieID)){
                return movie;
            }
        }
        return Movie.getNullMovie();
    }

    public Book getBookByISBNNumber(String isbnNumber){
        for(int i = 0;i<bookList.size();i++){
            Book book = bookList.get(i);
            if(book.getISBNNumber().equals(isbnNumber)){
                return book;
            }
        }
        return Book.getNullBook();
    }

    public List<Movie> getMovieList() {
        return movieList;
    }

    private boolean reserve(ReservableItem reservableItem, String customerName) {
        return reservableItem.reserve(customerName);
    }
}
